import { observable,computed, action, asMap, toJS } from 'mobx';
import {Tools,Bubble,Modal} from 'weaver-mobile';
import {getTheme,getEquipment} from '../util/pulic-func';
import * as API from '../api/fnaInvoiceScanning';
const weaAlert = Modal.alert;
const getLabel = Tools.getLabel;

export default class ScanningPageStore{
    @observable loding = false;

    //@observable layout = new RouteLayout.Store({ route: '/req', id: 'fnaInvoiceScanning' });
    @observable invoiceCloudInfo = {};
    @observable invoiceInfo = {};
    @observable fieldId = '';

    @observable enabled = {};
    @observable theme = {};
    @observable equipment = {};    

    @observable cloudInvoiceId = '';

    //获取token
    getInvoiceCloudToken = (value={},callBack) =>{
        API.getInvoiceCloudToken(value).then( res =>{
            let {status,invoiceCloud = false,errorInfo} = res;
            if(status === '1'){
                if(invoiceCloud){//发票云
                    if(errorInfo){
                        Bubble.error(errorInfo);
                    }else{
                        this.invoiceCloudInfo = {
                            token: res.token,
                            invoiceCloud: invoiceCloud,
                            jsUrl:res.jsUrl,
                        }
                    }
                }else{
                    this.invoiceCloudInfo = {
                        invoiceCloud: invoiceCloud
                    }
                }

                callBack && callBack(); 
                
            }else{
                Bubble.error(errorInfo);
            }
           
        }) 
           
    }   
    
    //发起报销单
    addDetail = (value,callBack)=>{
        this.loding = true;
        API.addDetail(value).then(res =>{
            const {status,msgBuffer} = res;
            if(status == 1){
                if(msgBuffer){
                    Bubble.error(msgBuffer)
                }
                this.invoiceInfo = {
                    fieldindex: res.fieldindex,
                    fieldIdDate: res.fieldIdDate,
                    fieldIdSubject: res.fieldIdSubject,
                    fieldIdAmount: res.fieldIdAmount,
                    fieldIdInvoice:res.fieldIdInvoice,
                    fieldIdDesc: res.fieldIdDesc,
                    idStr: res.idStr,
                    specialobj:res.specialobj
                }
                
                callBack && callBack({invoiceInfo:this.invoiceInfo,desc:res.desc});
            }else{
                weaAlert(res.errorInfo,'',[{text: getLabel(33703, '确认')}]);
            }
            this.loding = false;
        })
    } 

    //获取fieldId 
    getFieldId = (value={},callBack,failCallBack) =>{
        this.loding = true;
        API.getFieldId(value).then(res =>{
            const {status,fieldIdInvoice} = res;
            if(status == 1){
                this.fieldId = fieldIdInvoice;
                callBack && callBack({fieldId:fieldIdInvoice})
            }else{
                weaAlert('',res.errorInfo,[{text: getLabel(33703, '确认'),onPress: () => {failCallBack && failCallBack()}}]);
            }
            this.loding = false;
        })
    }

    //获取 发票录入的方式
    getEnterInvoiceTap = (value) =>{
        this.loding = true;
        API.getEnterInvoiceTap(value).then( res =>{
            const {status,invoiceCloud,data} = res;
            if(status == '1'){
                if(invoiceCloud){
                    const { Ocr,QRcode,EnterByHand,weChatImport } = invoiceCloud;
                    this.enabled = {
                        scanCode: QRcode,
                        photo:Ocr,
                        importFromWX: weChatImport,
                        validate:true
                    }
                }
            }
            this.loding = false;
            
        })

    }

    //获取主题
    getTheme = (value={}) =>{
        getTheme((_obj)=>{
            if(_obj){
                if(_obj.errCode == '0'){
                    this.theme = {
                        primary: _obj.clientTheme,
                        // ..._obj,
                    };
                }else{
                    Bubble.error(_obj);
                }
            }
        });

    }

    getEquipmentFn = () =>{
        this.equipment = getEquipment();
    }    

    /* ********************发票云*********************  */
    setCloudInvoiceId = (value) =>{
        this.cloudInvoiceId = value;
    } 
       

}